/**
 * 
 */
package org.tio.sitexxx.view.http.pathmodel;

import java.util.Map;

import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.http.server.util.Resps;
import org.tio.sitexxx.service.model.main.Blog;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.service.base.UserService;
import org.tio.sitexxx.service.service.tiosite.BlogService;
import org.tio.sitexxx.view.annotaion.ModelGeneratorPath;
import org.tio.sitexxx.web.server.utils.WebUtils;

import com.jfinal.plugin.activerecord.Record;

import cn.hutool.core.util.ObjectUtil;

/**
 * 更新某一篇博客
 * @author tanyaowu
 */
@ModelGeneratorPath({ "/blog/u/index.html" })
public class BlogUpdatePathModelGenerator implements PathModelGenerator {

	/**
	 * 
	 */
	public BlogUpdatePathModelGenerator() {
	}

	@Override
	public HttpResponse generate(HttpRequest request, String path, Map<Object, Object> baseModel) throws Exception {
		Integer blogid = request.getInt("id");

		Record blog = BlogService.me.getByIdWithMd(blogid);

		if (blog != null) {
			Integer uid = blog.get("uid");//Uid();
			//			User user = UserService.ME.getById(uid);  //博主

			//当前用户可能是null
			User curr = WebUtils.currUser(request);

			//当前用户和作者不是同一个人，且当前用户不是超管 
			if (curr == null || (ObjectUtil.notEqual(uid, curr.getId()) && !UserService.isSupper(curr) ) ) {
				return Resps.resp404(request);
			}

			//封停了
			if (ObjectUtil.equal(Blog.Status.SUSPENDED, blog.get("status"))) {
				return Resps.resp404(request);
			}
		} else {
			return Resps.resp404(request);
		}

		return null;
	}

	/**
	 * @param args
	 * @author tanyaowu
	 */
	public static void main(String[] args) {

	}

}
